<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\BannerService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class BannerController extends BaseController
{
    //轮播图

    //列表
    public function index(Request $request)
    {
        $type = $request->post('type', 1);
        $res = BannerService::index($type);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加
    public function modify(Request $request)
    {
        $icon = $request->post('img', '');
        if ($icon) {
            if (substr($icon, 0, 5) == 'data:') {
                $pas['img'] = Upload::getPathByBase64($icon);
            } else {
                $pas['img'] = $icon;
            }
        }
        $res = BannerService::add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = BannerService::del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}